from PyQt5.QtWidgets import QWidget, QGridLayout, QGroupBox, QLabel, QSpinBox, QPushButton
import re


class AttSpinBox(QSpinBox):
    def __init__(self):
        super().__init__()
        self.setRange(0, 300)


class SetupAttestatMarksSizes(QWidget):

    def save_settings(self):
        lines = open('templates/attestat-marks-template.html', 'r').readlines()
        for spinbox in self.info_about_sizes:
            comment = self.info_about_sizes[spinbox]
            for i in range(len(lines)):
                if comment in lines[i]:
                    value = re.findall(r'\d+', lines[i])[0]
                    lines[i] = lines[i].replace(value, str(spinbox.value()))
        with open('templates/attestat-marks-template.html', 'w') as out:
            out.write(''.join(lines))

    def __init__(self):
        super().__init__()

        grid = QGridLayout(self)
        self.setLayout(grid)

        grid.addWidget(QLabel('Все размеры указаны в мм'), 0, 0)

        size_groupbox = QGroupBox('Размеры приложения')
        size_groupbox_layout = QGridLayout()
        size_groupbox.setLayout(size_groupbox_layout)
        size_groupbox_layout.addWidget(QLabel('Ширина'), 0, 0)
        self.body_width_input = AttSpinBox()
        size_groupbox_layout.addWidget(self.body_width_input, 0, 1)
        size_groupbox_layout.addWidget(QLabel('Высота'), 1, 0)
        self.body_height_input = AttSpinBox()
        size_groupbox_layout.addWidget(self.body_height_input, 1, 1)

        grid.addWidget(size_groupbox, 1, 0)

        marks_groupbox = QGroupBox('Оценки')
        marks_groupbox_layout = QGridLayout()
        marks_groupbox.setLayout(marks_groupbox_layout)
        marks_groupbox_layout.addWidget(QLabel('Ширина столбца предметов'), 0, 0)
        self.disciplines_width_input = AttSpinBox()
        marks_groupbox_layout.addWidget(self.disciplines_width_input, 0, 1)
        marks_groupbox_layout.addWidget(QLabel('Ширина столбца оценок'), 1, 0)
        self.marks_width_input = AttSpinBox()
        marks_groupbox_layout.addWidget(self.marks_width_input, 1, 1)
        marks_groupbox_layout.addWidget(QLabel('Высота строки'), 2, 0)
        self.row_height_input = AttSpinBox()
        marks_groupbox_layout.addWidget(self.row_height_input, 2, 1)
        marks_groupbox_layout.addWidget(QLabel('Отступ слева первой таблицы'), 3, 0)
        self.first_table_left_input = AttSpinBox()
        marks_groupbox_layout.addWidget(self.first_table_left_input, 3, 1)
        marks_groupbox_layout.addWidget(QLabel('Отступ слева второй таблицы'), 4, 0)
        self.second_table_left_input = AttSpinBox()
        marks_groupbox_layout.addWidget(self.second_table_left_input, 4, 1)
        marks_groupbox_layout.addWidget(QLabel('Отступ сверху'), 5, 0)
        self.marks_top_input = AttSpinBox()
        marks_groupbox_layout.addWidget(self.marks_top_input, 5, 1)
        marks_groupbox_layout.addWidget(QLabel('Высота таблицы'), 6, 0)
        self.marks_height_input = AttSpinBox()
        marks_groupbox_layout.addWidget(self.marks_height_input, 6, 1)

        grid.addWidget(marks_groupbox, 2, 0)

        self.info_about_sizes = {
            self.body_width_input: 'body-width',
            self.body_height_input: 'body-height',
            self.disciplines_width_input: 'disciplines-width',
            self.marks_width_input: 'marks-width',
            self.row_height_input: 'tr-height',
            self.first_table_left_input: 'first-marks-left',
            self.second_table_left_input: 'second-marks-left',
            self.marks_top_input: 'table-top',
            self.marks_height_input: 'table-height'
        }

        inp = open('templates/attestat-marks-template.html', 'r')

        for spinbox in self.info_about_sizes:
            comment = self.info_about_sizes[spinbox]
            inp.seek(0)
            for line in inp:
                if comment in line:
                    value = int(re.findall(r'\d+', line)[0])
                    spinbox.setValue(value)

        save_button = QPushButton('Сохранить настройки')
        save_button.clicked.connect(self.save_settings)
        grid.addWidget(save_button)

        self.setMinimumWidth(500)
        self.setMinimumHeight(400)
        self.setWindowTitle('Настройка размеров приложения с оценками')
        self.show()
